package com.dancun.controller;


import com.dancun.entity.CTask;
import com.dancun.entity.Student;
import com.dancun.entity.TDocument;
import com.dancun.service.IStudentService;
import com.dancun.service.ITDocumentService;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.print.Doc;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-02
 */
@RestController
public class TDocumentController {
    @Autowired
    ITDocumentService tDocumentService;

    @RequestMapping("/listTDocument")
    public ModelAndView list(){
        ModelAndView mav = new ModelAndView("/Main/TdocumentList.jsp");
        List<TDocument> tDocument = tDocumentService.list();
        mav.addObject("list", tDocument);
        return mav;
    }
    @RequestMapping("/listTDocumentStu")
    public ModelAndView list1(){
        ModelAndView mav = new ModelAndView("/Main/TdocumentStu.jsp");
        List<TDocument> tDocument = tDocumentService.list();
        mav.addObject("list", tDocument);
        return mav;
    }


    @RequestMapping("/saveAdd")
    public String save(TDocument tDocument, @Param("date1") Long date1,HttpServletResponse response) throws IOException {
        tDocument.setDate(LocalDateTime.now());
        ModelAndView mav = null;
        tDocument.setDate(LocalDateTime.ofEpochSecond(date1,0, ZoneOffset.ofHours(8)));
        if (tDocumentService.save(tDocument)) {
            response.sendRedirect("listTDocument");

        } else {
            response.sendRedirect("listTDocument");
        }
        return null;
    }
    @RequestMapping("/fintDocumentJson")
    public String findtDocument(TDocument tDocument) throws IOException {
        TDocument t = tDocumentService.getById(tDocument.getTaskId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("attachment", t.getAttachment());
        jsonObject.put("date", t.getDate().toString().substring(0, 19));
        jsonObject.put("type", t.getType());
        jsonObject.put("content", t.getContent());


        return jsonObject.toString();
    }

    @GetMapping("/doc")
    public ModelAndView findOneDoc(String id) {
        ModelAndView mav = new ModelAndView("/Main/document.jsp");
        TDocument document = tDocumentService.getById(id);
        mav.addObject("doc", document);
        return mav;
    }

    @RequestMapping(value = "/find")
    public ModelAndView find(@Param("id") String id){
        ModelAndView mav = new ModelAndView("/Main/TdocumentUpdate.jsp");
        TDocument tDocument = tDocumentService.getById(id);
        mav.addObject("tDocument",tDocument);
        return mav;
    }

    @RequestMapping("/updatedoc")
    public ModelAndView update(TDocument tDocument) {
        ModelAndView mav = new ModelAndView("/listTDocument");
        if(tDocumentService.updateById(tDocument)){
            return mav;
        }
        mav.setViewName("/listTDocument");
        return mav;
    }
    @RequestMapping("/delDoc")
    public String delete(@Param("id") String taskId,HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(tDocumentService.removeById(taskId)){
            out.print("<script>alert('删除成功');window.location.href='listTDocument'</script>");
        }else{
            out.print("<script>alert('删除失败');window.location.href='listTDocument'</script>");
        }
        return null;

    }
}
